/*
 * @lc app=leetcode.cn id=112 lang=cpp
 *
 * [112] 路径总和
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;
// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int target;
    bool dfs(TreeNode* root, int current) {
        if(current == target && !root->left && !root->right) {
            return true;
        }
        if(root->left) {
            if(dfs(root->left,current + root->left->val)) {
                return true;
            }
        }
        if(root->right) {
            if( dfs(root->right,current + root->right->val)) {
                return true;
            }
           
        }
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(!root) {
            return false;
        }
        target = targetSum;
        return dfs(root,root->val);
    }
};
// @lc code=end

